<template>
  <view class="admin-container">
    <!-- 顶部渐变标题区 -->
    <view class="admin-header">
      <view class="admin-profile">
        <image class="admin-avatar" :src="user.avatar || '/static/default-avatar.png'" mode="aspectFill" />
        <view class="admin-greeting">
          <text class="admin-title">管理员中心</text>
          <text class="admin-name">{{ user.name || '管理员' }}</text>
        </view>
      </view>
      <view class="header-wave"></view>
    </view>

    <!-- 功能菜单卡片 -->
    <view class="admin-menu">
      <!-- 题库管理 -->
      <view 
        class="menu-card"
        @click="navigateTo('/pages/admin/upload-questions')"
        @touchstart="cardScale(0.98, 'q')"
        @touchend="cardScale(1, 'q')"
      >
        <view class="card-icon" style="background-color: rgba(74, 144, 226, 0.1);">
          <uni-icons type="compose" size="28" color="#4a90e2"></uni-icons>
        </view>
        <view class="card-content">
          <text class="card-title">题库管理</text>
          <text class="card-desc">上传和管理试题资源</text>
        </view>
        <uni-icons type="right" size="18" color="#ccc"></uni-icons>
      </view>

      <!-- 奖品管理 -->
      <view 
        class="menu-card"
        @click="navigateTo('/pages/admin/upload-items')"
        @touchstart="cardScale(0.98, 'p')"
        @touchend="cardScale(1, 'p')"
      >
        <view class="card-icon" style="background-color: rgba(255, 152, 0, 0.1);">
          <uni-icons type="gift" size="28" color="#FF9800"></uni-icons>
        </view>
        <view class="card-content">
          <text class="card-title">奖品管理</text>
          <text class="card-desc">配置兑换奖品</text>
        </view>
        <uni-icons type="right" size="18" color="#ccc"></uni-icons>
      </view>

      <!-- 用户管理 -->
      <view 
        class="menu-card"
        @click="navigateTo('/pages/admin/manage-users')"
        @touchstart="cardScale(0.98, 'u')"
        @touchend="cardScale(1, 'u')"
      >
        <view class="card-icon" style="background-color: rgba(156, 39, 176, 0.1);">
          <uni-icons type="person" size="28" color="#9C27B0"></uni-icons>
        </view>
        <view class="card-content">
          <text class="card-title">用户管理</text>
          <text class="card-desc">管理系统用户账户</text>
        </view>
        <uni-icons type="right" size="18" color="#ccc"></uni-icons>
      </view>
      
      <!-- 视频管理 -->
      <view 
        class="menu-card"
        @click="navigateTo('/pages/admin/VideoManagement')"
        @touchstart="cardScale(0.98, 'd')"
        @touchend="cardScale(1, 'd')"
      >
        <view class="card-icon" style="background-color: rgba(0, 178, 24, 0.1);">
          <uni-icons type="videocam" size="28" color="#00b218"></uni-icons>
        </view>
        <view class="card-content">
          <text class="card-title">视频管理</text>
          <text class="card-desc">管理学习视频</text>
        </view>
        <uni-icons type="right" size="18" color="#ccc"></uni-icons>
      </view>
    </view>

    <!-- 底部操作栏 -->
    <view class="admin-footer">
      <button 
        class="logout-button"
        @click="logout"
        @touchstart="buttonScale(0.98)"
        @touchend="buttonScale(1)"
      >
        <view class="button-content">
          <uni-icons type="logout" size="20" color="#FF5252"></uni-icons>
          <text>退出登录</text>
        </view>
      </button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      user: {}
    }
  },
  onLoad() {
    this.loadUserData()
  },
  methods: {
    loadUserData() {
      const user = uni.getStorageSync('user') || {}
      if (!user.id || user.role !== 'admin') {
        uni.redirectTo({ url: '/pages/student/login' })
      } else {
        this.user = user
      }
    },
    navigateTo(url) {
      uni.navigateTo({ url })
    },
    logout() {
      uni.showModal({
        title: '退出确认',
        content: '您确定要退出管理员账户吗？',
        confirmColor: '#FF5252',
        success: (res) => {
          if (res.confirm) {
            uni.removeStorageSync('user')
            uni.redirectTo({ url: '/pages/student/login' })
          }
        }
      })
    },
    cardScale(scale, key) {
      const query = uni.createSelectorQuery().in(this)
      query.select(`.menu-card:nth-child(${key === 'q' ? 1 : key === 'p' ? 2 : 3})`)
        .boundingClientRect()
        .exec(res => {
          if (res[0]) {
            res[0].animate([{ transform: `scale(${scale})` }], { 
              duration: 200,
              fill: 'forwards' 
            })
          }
        })
    },
    buttonScale(scale) {
      const query = uni.createSelectorQuery().in(this)
      query.select('.logout-button')
        .boundingClientRect()
        .exec(res => {
          if (res[0]) {
            res[0].animate([{ transform: `scale(${scale})` }], { 
              duration: 200,
              fill: 'forwards' 
            })
          }
        })
    }
  }
}
</script>

<style scoped>
/* 全局容器 */
.admin-container {
  position: relative;
  min-height: 100vh;
  background-color: #f8f9fa;
  display: flex;
  flex-direction: column;
}

/* 顶部标题区 */
.admin-header {
  padding: 40rpx 30rpx 80rpx;
  background: linear-gradient(135deg, #4a90e2, #5aa1ff);
  color: white;
  position: relative;
  overflow: hidden;
  border-radius: 0 0 40rpx 40rpx;
}

.header-wave {
  position: absolute;
  bottom: -20rpx;
  left: 0;
  right: 0;
  height: 40rpx;
  background: url('data:image/svg+xml;utf8,<svg viewBox="0 0 1200 120" xmlns="http://www.w3.org/2000/svg" preserveAspectRatio="none"><path d="M0,0V46.29c47.79,22.2,103.59,32.17,158,28,70.36-5.37,136.33-33.31,206.8-37.5C438.64,32.43,512.34,53.67,583,72.05c69.27,18,138.3,24.88,209.4,13.08,36.15-6,69.85-17.84,104.45-29.34C989.49,25,1113-14.29,1200,52.47V0Z" fill="%23f8f9fa" opacity=".25"/></svg>');
  background-size: cover;
}

.admin-profile {
  display: flex;
  align-items: center;
  z-index: 2;
  position: relative;
}

.admin-avatar {
  width: 120rpx;
  height: 120rpx;
  border-radius: 50%;
  border: 4rpx solid rgba(255,255,255,0.8);
  box-shadow: 0 10rpx 30rpx rgba(0,0,0,0.1);
}

.admin-greeting {
  margin-left: 30rpx;
}

.admin-title {
  font-size: 40rpx;
  font-weight: 700;
  display: block;
  line-height: 1.4;
}

.admin-name {
  font-size: 28rpx;
  opacity: 0.9;
  display: block;
}

/* 功能菜单 */
.admin-menu {
  flex: 1;
  padding: 0 30rpx;
  margin-top: -40rpx;
  position: relative;
  z-index: 2;
  margin-bottom: 120rpx;
}

.menu-card {
  display: flex;
  align-items: center;
  background-color: #fff;
  border-radius: 20rpx;
  padding: 30rpx;
  margin-bottom: 24rpx;
  box-shadow: 0 8rpx 24rpx rgba(0,0,0,0.05);
  transition: all 0.3s;
}

.card-icon {
  width: 88rpx;
  height: 88rpx;
  border-radius: 20rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 24rpx;
}

.card-content {
  flex: 1;
}

.card-title {
  font-size: 32rpx;
  font-weight: 600;
  color: #333;
  line-height: 1.4;
}

.card-desc {
  font-size: 24rpx;
  color: #999;
}

/* 底部操作栏 */
.admin-footer {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 20rpx 30rpx;
  background-color: #fff;
  box-shadow: 0 -4rpx 20rpx rgba(0,0,0,0.05);
  z-index: 3;
}

.logout-button {
  height: 88rpx;
  background-color: rgba(255, 82, 82, 0.05);
  border-radius: 16rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #FF5252;
  font-size: 30rpx;
  border: 1rpx solid rgba(255, 82, 82, 0.2);
  transition: all 0.3s;
}

.logout-button:active {
  background-color: rgba(255, 82, 82, 0.1);
}

.button-content {
  display: flex;
  align-items: center;
  gap: 12rpx;
}

/* 动画效果 */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(20rpx); }
  to { opacity: 1; transform: translateY(0); }
}

.menu-card {
  animation: fadeIn 0.5s ease-out forwards;
}

.menu-card:nth-child(1) { animation-delay: 0.1s; }
.menu-card:nth-child(2) { animation-delay: 0.2s; }
.menu-card:nth-child(3) { animation-delay: 0.3s; }
.menu-card:nth-child(4) { animation-delay: 0.4s; }
</style>